import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:package_info/package_info.dart';
import 'package:common_utils/common_utils.dart';

class MyUtils {
  static showToast(String msg) {
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIos: 1,
        backgroundColor: Colors.black45,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  /*
   *  用于，截取 chart 最左边的标题；
   *  避免太长；
   *
   */

  static getChartLeftTitle(String titleValue) {
    if (titleValue.length <= 6) {
      return titleValue;
    }
    if (titleValue.length > 6 && titleValue.length < 12) {
      return titleValue.substring(0, 5) + '\n' + titleValue.substring(7);
    }

    if (titleValue.length > 12) {
      return titleValue.substring(0, 5) +
          '\n' +
          titleValue.substring(7, 11) +
          '...';
    }
    return titleValue;
  }

  /*
   *   获取本地app版本；
   *
   */

  static Future<String> getNowAppVerCode() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    return packageInfo.version;
  }

  /*
   * 获取当月的最后一天；
   */
  static String getNowMonthLastDay() {
    DateTime dateTime = DateTime.now();
    int nowYear = dateTime.year;
    int nowMonth = dateTime.month;
    // 下个月的第一天；
    int nextYear;
    int nextMonth;
    int nextDay = 1;
    nextYear = nowMonth == 12 ? nowYear + 1 : nowYear;
    nextMonth = nowMonth == 12 ? 1 : nowMonth + 1;
    DateTime nextDate = DateTime(nextYear, nextMonth, nextDay);
    //当月的最后一天；
    DateTime nowMonthLastDay = nextDate.add(Duration(days: -1));
    return DateUtil.getDateStrByDateTime(nowMonthLastDay,
        format: DateFormat.YEAR_MONTH_DAY);
  }

  //对金额，进行 四舍五入，并格式化千位符输出。

  static String numFormat(String value) {
    List<String> values = value.split('.');
    String intValue = values[0];
    String otherValue = values[1];

    //四舍五入；
    if (otherValue.length >= 3) {
      List<String> others = otherValue.split('');

      String two = others[1];
      String three = others[2];
      int threeValue = int.parse(three);
      if (threeValue >= 5) {
        int towValue = int.parse(two) + 1;
        two = '$towValue';
      }
      others[1] = two;
      others[2] = three;
      otherValue = otherValue[0] + otherValue[1];
    } else if (otherValue.length == 1) {
      otherValue = otherValue + '0';
    } else if (otherValue.length == 0) {
      otherValue = '00';
    }
    //整数部分千位符；

    List<String> ints = intValue.split('');
    if (ints.length > 3) {
      int index = 0;
      for (int i = ints.length - 1; i >= 0; i--) {
        index = index + 1;
        if (index % 3 == 0 && i != 0) {
          ints[i] = ',' + ints[i];
          index = 0;
        }
      }

      intValue = ints.join();
    }

    return intValue + '.' + otherValue;
  }

  //四舍五入；
  static String sswr(String value) {
    //四舍五入；
    if (value.length >= 3) {
      List<String> others = value.split('');

      String two = others[1];
      String three = others[2];
      int threeValue = int.parse(three);
      if (threeValue >= 5) {
        int towValue = int.parse(two) + 1;
        two = '$towValue';
      }
      others[1] = two;
      others[2] = three;
      value = value[0] + value[1];
    } else if (value.length == 1) {
      value = value + '0';
    } else if (value.length == 0) {
      value = '00';
    }
    return value;
  }

  static String floatFormat(String value) {
    if (value.isEmpty) {
      return '0';
    } else {
      List<String> valueItems = value.split('.');
      if (valueItems.length == 2) {
        String two = sswr(valueItems[1]);

        return valueItems[0] + '.' + two;
      } else {
        return valueItems[0] + '.00';
      }
    }
  }

  static String intFormat(String value) {
    if (value.isEmpty) {
      return '0';
    } else {
      List<String> valueItems = value.split('.');
      return valueItems[0];
    }
  }

  static String addNing(int value) {
    if (value < 10) return '0$value';
    return '$value';
  }
}
